package com.fg.cloud.manager.service.agency.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.NumberUtil;
import com.fg.cloud.common.finals.AgencyBill;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.AgencyAccountEntity;
import com.fg.cloud.manager.entry.agency.AgencyBillEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyAccountMapper;
import com.fg.cloud.manager.mapper.agency.AgencyBillMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.agency.AgencyAccountService;

/**
  * @Description:经销商账户接口实现
  * @author dingjiang
  * @date 2017年8月16日上午10:58:11
*/
@Service
public class AgencyAccountServiceImpl extends BaseServiceImpl implements AgencyAccountService {

	@Autowired
	private AgencyAccountMapper agencyAccountMapper;
	@Autowired
	private AgencyBillMapper billMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return agencyAccountMapper;
	}


	/**
	 * 动态查询经销商账户
	 */
	@Override
	public AgencyAccountEntity selectBycondition(AgencyAccountEntity accountEntity) {
		
		return agencyAccountMapper.selectBycondition(accountEntity);
	}

	/**
	 * 判断账户是否已经存在
	 */
	@Override
	public boolean judgeIsExist(AgencyAccountEntity agencyAccount) {

		return agencyAccountMapper.judgeIsExist(agencyAccount)>0 ? true : false ;
	}

	@Override
	@Transactional
	public boolean modify(AgencyAccountEntity accountEntity,BigDecimal number,AgencyBillEntity billEntity2) {
		AgencyBillEntity billEntity = new AgencyBillEntity();
		billEntity.setFacId(accountEntity.getFacId());
		billEntity.setTransTime(new Date());
		billEntity.setAgeId(accountEntity.getAgeId());
		billEntity.setConfirmTime(new Date());
		billEntity.setAmount(number);
		billEntity.setConfirmAmount(number);
		billEntity.setStatus(AgencyBill.STATUS);
		billEntity.setRemark(billEntity2.getRemark());
		billEntity.setTransMode(AgencyBill.OFFLINE);
		if(accountEntity.getType()==2){
			billEntity.setTransInfo(AgencyBill.AMOUNT_ADD);
			billEntity.setTransMode(AgencyBill.OFFLINE);
			billEntity.setCollectionAccount(billEntity2.getCollectionAccount());
		}else{
			billEntity.setTransInfo(AgencyBill.UP_USABLE);
		}
		//编码规则
		billEntity.setRunningNum(NumberUtil.getSerialNumber());
		billMapper.insert(billEntity);
		return agencyAccountMapper.update(accountEntity)>0;
	}
	
	@Override
	@Transactional
	public boolean modifyLimit(AgencyAccountEntity accountEntity,BigDecimal number,BigDecimal oldLimit,String admin) {
		AgencyBillEntity billEntity = new AgencyBillEntity();
		billEntity.setFacId(accountEntity.getFacId());
		billEntity.setTransTime(new Date());
		billEntity.setAgeId(accountEntity.getAgeId());
		billEntity.setConfirmTime(new Date());
		billEntity.setAdmin(admin);
		if(accountEntity.getLimitUsable()!=null){
			billEntity.setAmount(number.subtract(oldLimit));
			billEntity.setConfirmAmount(number.subtract(oldLimit));
			billEntity.setStatus(AgencyBill.STATUS);
			billEntity.setTransInfo(AgencyBill.UP_LIMIT);
			//编码规则
			billEntity.setRunningNum(NumberUtil.getSerialNumber());
			billMapper.insert(billEntity);
			
			billEntity.setTransInfo(AgencyBill.UP_USABLE);
			//编码规则
			billEntity.setRunningNum(NumberUtil.getSerialNumber());
			billMapper.insert(billEntity);
		}else{
			billEntity.setAmount(oldLimit.subtract(number));
			billEntity.setConfirmAmount(oldLimit.subtract(number));
			billEntity.setStatus(AgencyBill.STATUS);
			billEntity.setTransInfo(AgencyBill.DOWN_LIMIT);
			
			//编码规则
			billEntity.setRunningNum(NumberUtil.getSerialNumber());
			billMapper.insert(billEntity);
		}
		return agencyAccountMapper.update(accountEntity)>0;
	}

	@Override
	public List<AgencyAccountEntity> findAccount(AgencyAccountEntity accountEntity) {
		return agencyAccountMapper.selectAccount(accountEntity);
	}
}
